/**
 * 
 */
package com.itboye.textileexpres.base;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.alibaba.mobileim.YWAPI;
import com.alibaba.wxlib.util.SysUtil;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.util.IsMobileNumPhone;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.common.UmLog;
import com.umeng.message.entity.UMessage;

/**
 * @author hebidu
 * 
 */
public class MainApplication extends Application {

	public static Context context;
	public static List<WeakReference<Activity>> activitys = new LinkedList<>();
	private static RequestQueue queue;
	private static MainApplication instance;

	private static MainApplication getInstance;

	// 友盟
	public PushAgent mPushAgent;

	public PushAgent getmPushAgent() {
		return mPushAgent;
	}

	// public LocationService locationService;
	// public Vibrator mVibrator;

	public static MainApplication getGetInstance() {
		if (instance == null) {
			instance = new MainApplication();
		}
		return getInstance;
	}

	JSONObject jsonObject;

	protected int type;

	protected String uid;

	protected String nickName;
	private static final String TAG = MainApplication.class.getName();
	public static final String UPDATE_STATUS_ACTION = "com.itboye.textileexpres.action.UPDATE_STATUS";

	public static void setGetInstance(MainApplication getInstance) {
		MainApplication.getInstance = getInstance;
	}

	public static MainApplication getInstance() {
		return getInstance;
	}
	public void addActivity(Activity activity) {
		activitys.add(new WeakReference<Activity>(activity));
	}

	@Override
	public void onCreate() {
		super.onCreate();
		// CrashHandler.getInstance().init(getApplicationContext());
		getInstance = this;
		
		context = getApplicationContext();

		if (mustRunFirstInsideApplicationOnCreate()) {
			// todo 如果在":TCMSSevice"进程中，无需进行openIM和app业务的初始化，以节省内存
			return;
		}
		// 配置阿里百川
		SysUtil.setApplication(this);
		YWAPI.init(this, IsMobileNumPhone.BauChuanKey);
		queue = Volley.newRequestQueue(context);
//		initUMengPush();
		}
/**
 * youmeng
 */
		private void initUMengPush() {
			mPushAgent = PushAgent.getInstance(this);
			mPushAgent.setDebugMode(true);
			UmengMessageHandler messageHandler = new UmengMessageHandler() {
				/**
				 * 自定义消息的回调方法
				 * */
				@Override
				public void dealWithCustomMessage(final Context context,
						final UMessage msg) {
					new Handler().post(new Runnable() {

						@Override
						public void run() {
							// TODO Auto-generated method stub
							// 对自定义消息的处理方式，点击或者忽略
							boolean isClickOrDismissed = true;
							if (isClickOrDismissed) {
								// 自定义消息的点击统计
								UTrack.getInstance(getApplicationContext())
										.trackMsgClick(msg);
							} else {
								// 自定义消息的忽略统计
								UTrack.getInstance(getApplicationContext())
										.trackMsgDismissed(msg);
							}
							Toast.makeText(context, msg.custom, Toast.LENGTH_LONG)
									.show();
						}
					});
				}

				/**
				 * 自定义通知栏样式的回调方法
				 * */
				@SuppressLint("NewApi")
				@Override
				public Notification getNotification(Context context, UMessage msg) {
					switch (msg.builder_id) {
					case 1:
						Notification.Builder builder = new Notification.Builder(
								context);
						RemoteViews myNotificationView = new RemoteViews(
								context.getPackageName(),
								R.layout.notification_view);
						myNotificationView.setTextViewText(R.id.notification_title,
								msg.title);
						myNotificationView.setTextViewText(R.id.notification_text,
								msg.text);
						myNotificationView.setImageViewBitmap(
								R.id.notification_large_icon,
								getLargeIcon(context, msg));
						myNotificationView.setImageViewResource(
								R.id.notification_small_icon,
								getSmallIconId(context, msg));
						builder.setContent(myNotificationView)
								.setSmallIcon(getSmallIconId(context, msg))
								.setTicker(msg.ticker).setAutoCancel(true);

						return builder.getNotification();
					default:
						// 默认为0，若填写的builder_id并不存在，也使用默认。
						return super.getNotification(context, msg);
					}
				}
			};
			mPushAgent.setMessageHandler(messageHandler);

			/**
			 * 自定义行为的回调处理 UmengNotificationClickHandler是在BroadcastReceiver中被调用，故
			 * 如果需启动Activity，需添加Intent.FLAG_ACTIVITY_NEW_TASK
			 * */
			UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
				@Override
				public void dealWithCustomAction(Context context, UMessage msg) {
					Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
				}

				@Override
				public void openActivity(Context arg0, UMessage arg1) {
					// TODO Auto-generated method stub
					super.openActivity(arg0, arg1);
					System.out.println(">>>通知栏消息" + arg1.getRaw());
				}
			};
			// 使用自定义的NotificationHandler，来结合友盟统计处理消息通知
			// 参考http://bbs.umeng.com/thread-11112-1-1.html
			// CustomNotificationHandler notificationClickHandler = new
			// CustomNotificationHandler();
			mPushAgent.setNotificationClickHandler(notificationClickHandler);

			// 注册推送服务 每次调用register都会回调该接口
			mPushAgent.register(new IUmengRegisterCallback() {
				@Override
				public void onSuccess(String deviceToken) {
					System.out.println(">>>" + deviceToken);
				}

				@Override
				public void onFailure(String s, String s1) {

				}
			});

		}

	private boolean mustRunFirstInsideApplicationOnCreate() {
		// 必须的初始化
		SysUtil.setApplication(this);
		return SysUtil.isTCMSServiceProcess(getApplicationContext());
	}

	public static Context getContext() {

		return context;
	}

	/**
	 * @return the queue
	 */
	public static RequestQueue getQueue() {
		return queue;
	}

	public static <T> void addRequest(Request<T> request) {
		request.addMarker("itboye");
		getQueue().add(request);
	}

}
